package org.study.article.web;

import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.study.article.model.ArticleRecord;
import org.study.article.model.vo.ArticleRecordSubmitVo;
import org.study.article.service.ArticleService;
import org.study.common.model.Message;

import java.util.List;

@RestController
@RequestMapping("/article")
@Slf4j
public class ArticleController {

    @Autowired
    ArticleService articleService;
    @RequestMapping("/insert")
    public String insert(@RequestBody ArticleRecordSubmitVo data){
        log.info("添加文章:"+ JSON.toJSONString(data));
        articleService.insert(data);
        return JSON.toJSONString(new Message(1,"添加成功"));
    }
    @RequestMapping("/listAll")
    public String listAll(){
        return JSON.toJSONString(articleService.listAll());
    }

    @RequestMapping("/{id}/detail")
    public ArticleRecord detail(@PathVariable("id") Long id){
        return articleService.selectByPrimaryKey(id);
    }

    @RequestMapping("/update")
    public String update(@RequestBody ArticleRecordSubmitVo data){
        log.debug("修改文章信息"+JSON.toJSONString(data));
        articleService.update(data);
        return JSON.toJSONString(new Message(1,"update.success"));
    }

}
